package com.huanzhidadi.single.compare;

import lombok.Data;
import lombok.ToString;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 实现Comparable接口排序
 */
public class TreeSetSortTest2 {

    public static void main(String[] args) {
        List<Person2> list = createData();
        System.out.println("排序前");
        list.stream().forEach(System.out::println);

        Collections.sort(list);
        System.out.println("排序后");
        list.stream().forEach(System.out::println);
    }

    public static List<Person2> createData() {
        List<Person2> list = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            Person2 p = new Person2();
            p.setId(i);
            p.setName("抡砖" + i + "号");
            p.setScore(107 - i);

            list.add(p);
        }

        for (int i = 10; i >= 5; i--) {
            Person2 p = new Person2();
            p.setId(i);
            p.setName("抡砖" + i + "号");
            p.setScore(100 + i);

            list.add(p);
        }

        return list;
    }
}

@Data
@ToString
class Person2 implements Comparable<Person2> {
    private String name;
    private int id;
    private int score;

    @Override
    public int compareTo(Person2 o) {
        /*if (this.id > o.getId()) {
            return 1;
        } else {
            return -1;
        }*/
        return this.id < o.getId() ? 1 : -1;
    }
}